﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
using TotalLMS.Util;

namespace TotalLMS.Datamodels.cmi
{
    public class CMIComments : CMICategory, ISerializable
    {
        // Student comments
        public Element comments;

        /****************************************************************************
         **
         ** Method:   Constructor
         ** Input:    String comments - Comments from the AU or student
         ** Output:   none
         **
         ** Description: A constructor used to initialize the comments.  This
         **              constructor takes in a String of comments to use for
         **              initialization.
         **
         ***************************************************************************/
        public CMIComments(String commentsString)
        {
            base(true);

            comments = new Element(commentsString, "checkString4096", "NULL", true, true, false);

        } // end of constructor

        /****************************************************************************
         **
         ** Method:   Default Constructor
         ** Input:    none
         ** Output:   none
         **
         ** Description:  Sets up the comments attribute with a default value.
         **
         ***************************************************************************/
        public CMIComments()
        {
            base(true);

            comments = new Element("", "checkString4096", "NULL", true, true, false);

        } // end of default constructor


        /************************************************************************
         **  Accessors to the CMIComments Data.  AUs should not invoke these
         **  methods.  AUs should call LMSGetValue()
         ************************************************************************/
        public Element getComments()
        {
            return comments;
        }

        /************************************************************************
         **  Modifiers to the CMIComments Data.  AUs should not invoke these
         **  methods.  AUs should call LMSSetValue()
         ************************************************************************/
        public void setComments(String inComments)
        {
            comments.setValue(inComments);
        }

        /****************************************************************************
         **
         ** Method:  performGet
         ** Input:   CMIRequest theRequest - tokenized LMSGetValue() request
         **          DMErrorManager dmErrorMgr - Error manager
         **
         ** Output:  String - the value portion of the element for the LMSGetValue()
         **
         ** Description:  This method performs the necessary steps to retrieve the
         **     value for the Comments data model element.
         **
         ***************************************************************************/
        public String performGet(CMIRequest theRequest, DMErrorManager dmErrorMgr)
   {
      // String to hold the value of the final element
      String result = "";

      // Check to see if the Request has more tokens to process
      if ( theRequest.hasMoreTokensToProcess() )
      {
         // Error - Data Model Element not implemented
         // *** There are no more subcategories in the CMI Comments class
         // *** Since there are more tokens to process, the data model
         // *** element was not implemented
         if ( DebugIndicator.ON )
         {
            Console.WriteLine("Error - Data Model Element not implemented");
            Console.WriteLine("Element being processed: " + theRequest.getElement() + "is not a valid data model element.\n");
         }
         // Determine if the Request is for a keyword.
         if ( theRequest.isAKeywordRequest() )
         {
            dmErrorMgr.recGetKeyWordError(theRequest.getElement());
         }
         else
         {
            dmErrorMgr.recNotImplementedError(theRequest);
         }
      }
      else
      {
         // No more tokens to process

         // determine the value associated with the element requested
         result = comments.getValue();
      }

      // Done getting requested element.  Let CMIRequest object know that
      // processing of the LMSGetValue() is complete
      theRequest.done();

      return result;
   }  // end of performGet()

        /****************************************************************************
         **
         ** Method:  performSet
         ** Input:   CMIRequest theRequest - tokenized LMSSetValue() request
         **          DMErrorManager dmErrorMgr - Error manager
         **
         ** Output:  none
         **
         ** Description:  This method performs the necessary steps to set the
         **     Comments Data Model Element.
         **     The comments are collected as a String not to exceen 4096 bytes
         **
         ***************************************************************************/
        public void performSet(CMIRequest theRequest, DMErrorManager dmErrorMgr)
   {

      // Check to see if the Request has more tokens to process
      if ( !theRequest.hasMoreTokensToProcess() )
      {

         // get the value to use for setting off of the request
         String content = theRequest.getValue();

         // Check to make sure that the string to be used to set the comments
         // does not exceed 4096 bytes.
         if ( content.length() <= 4096)
         {
            // get the current student comments
            String currentComments = comments.getValue();

            // Concatenate the comments together
            currentComments += content;

            // set the student comments with the new concatenated
            // comments
            comments.setValue(currentComments);
         }
         else
         {
            if ( DebugIndicator.ON )
            {
               Console.WriteLine("***** INVALID LMSSetValue() CALL *****");
               Console.WriteLine("             Invalid Type             ");
               Console.WriteLine("         Element was not set!         ");
            }
            dmErrorMgr.SetCurrentErrorCode("405");
         }

      }
      else
      {
         // Error - Data Model Element not implemented
         if ( DebugIndicator.ON )
         {
            Console.WriteLine("Error - Data Model Element not implemented");
            Console.WriteLine("Element being processed: " +
                               theRequest.getElement() +
                               "is not a valid data model element.\n");
         }

         dmErrorMgr.recNotImplementedError(theRequest);

      }

      // Done setting requested element.  Let CMIRequest object know that
      // processing of the LMSSetValue() is complete
      theRequest.done();

   } // end of performSet()


        #region ISerializable Members

        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            throw new NotImplementedException();
        }

        #endregion
    } // end of CMIComments

}
